package com.bookmanagement.controller.user;


import com.bookmanagement.DTO.BookDTO;
import com.bookmanagement.DTO.BookSearchDTO;
import com.bookmanagement.entity.Book;
import com.bookmanagement.result.PageResult;
import com.bookmanagement.result.Result;
import com.bookmanagement.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author skyjay
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/recommend")
    public Result<List<Book>> getRecommendBooks(){
        List<Book> books = bookService.getRecommendBooks();
        return Result.success(books);
    }

    @GetMapping("/{id}")
    public Result<BookDTO> getBookById(@PathVariable("id") Integer id){
        BookDTO bookById = bookService.getBookById(id);
        return Result.success(bookById);
    }

    @PostMapping("/search")
    public Result<PageResult> searchBook(@RequestBody BookSearchDTO bookSearchDTO){
        List<BookDTO> bookDTOs = bookService.searchBook(bookSearchDTO);
        PageResult pageResult = PageResult.builder()
                .total(bookDTOs.size())
                .records(bookDTOs)
                .build();
        return Result.success(pageResult);
    }
}
